package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentInviteBidsBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallBidingAdapter;
import com.sgb.kjwl.viewmodel.home.InviteBidsModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
/**
 * Description:
 * Author cfk
 * Time
 * 招标竞价中的招标fragment
 */
public class InviteBidsFragment extends AppLazyFragment {

    private FragmentInviteBidsBinding binding;
    private MainActivity activity;
    @Inject
    InviteBidsModel viewModel;
    private PickHallBidingAdapter pickingAdapter;
    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    //类型
    private String typeNo = "0";
    private int page = 1;
    private boolean isLoadMore = false;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }
    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_invite_bids, container, false);
        return binding.getRoot();

    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        binding.tvMoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isLoadMore = true;
                page++;
                viewModel.getList(typeNo,page);
            }
        });
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                typeNo = typeDatas.get(tab.getPosition()).getNo();
                pickingAdapter.setType(typeNo);
                getData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        if (pickingAdapter == null) {
            pickingAdapter = new PickHallBidingAdapter(getActivity(), null);
        }
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide1));
        binding.recyclerView.addItemDecoration(divider);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(pickingAdapter);
        pickingAdapter.setOnItemClick(new AdapterOnItemClick<TenderListEntity>() {
            @Override
            public void onItemClick(TenderListEntity tenderListEntity, int position) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId(), 0);
            }
        });
        pickingAdapter.onIChildClick(new PickHallBidingAdapter.IChildClick() {
            @Override
            public void onClick(TenderListEntity tenderListEntity) {
                TenderDetailsActivity3.start(getActivity(), tenderListEntity.getId(), 0);
            }
        });
        viewModel.getFilterTypeData();
    }

    @Override
    protected void onLazyLoad() {
        getData();
    }

    private void getData() {
        isLoadMore = false;
        page = 1;
        viewModel.getList(typeNo,page);
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        typeDatas.clear();
        typeDatas.addAll(datas);
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(typeDatas.get(i).getProjectscale()));
        }
        getData();
    }

    public void showList(List<TenderListEntity> list) {
        if (isLoadMore) {
            if (list.size() > 0) {
                pickingAdapter.addDatas(list);
            }
        } else {
            if (list.size() > 0) {
                pickingAdapter.setDatas(list);
            } else {
                pickingAdapter.setDatas(null);
            }
        }
        if (list.size() <= 2){
            binding.tvMoreData.setVisibility(View.GONE);
        }else{
            binding.tvMoreData.setVisibility(View.VISIBLE);
        }
    }

    public void getUrl(String url, String id) {
        WebViewX5HostActivity.start(getActivity(), url, id);
    }
}
